June 3rd, 2016 by Arthur Chen

Learning outcomes -- Edited by Arthur Chen on June 3rd


Today we first reviewed yesterday's homework section --- chapter two of the book How to think like a computer scientist. In this section, we learnt about the term 'variables', its categories, or class, and its properties. You can click me to see that chapter.

There are several classes that we should pay attention to:
name
abbreviation
meaning
converter symble() / style
example
operation
integer
int
integers
change base
int()
int( number , base )
6
int(11,2)
same as in maths
int(11,2) gives 3
float
float
decimals
float()
2.333
3.0-3.4 gives -0.39999
string
str
a sequence of characters
str() / ' '
a = 'string'
'a' + 'b' = 'ab' ≠ 'b' + 'a'
a[1] gives 't'
boolean
bool
True or False
bool()
True
True or False
dictionary
dict
/
{ }
{cool:'me',fun:'CS'}
cool gives 'me'
tuple
tuple
/
tuple() / ( )
a = (1,2,3,4,5)
a[1] gives 2
list
list
a list of elements
list() / [ ]
a = [1,2,3,4,5]
a[1] gives 2

ATTENTION:
  1. Python is Case Sensitive, so True is very different from true.
  2. Python does NOT need declaration, data type can be changed along the process.

Then we talked about statements, which are instructions that the Python interpreter can execute. The statements are always on the right side of assignment expressions. Note that the code:
<span style="font-size: 13.333333015441895px;">a = 10</span>
means a is given the value 10, so it reads: "a is assigned 10."
Also note that when we are naming a variable, we should follow several rules:
  1. We should pay attention to the capital letters, as said before, Python is case sensitive.
  2. We CANNOT begin the name with numbers, it will make a syntax error.
  3. We should NOT begin the name with _ as they usually mean something else.
  4. We should avoid using Python Keywords as follow:
 2016-06-03 下午9.07.58.png
<span style="font-size: 13.333333015441895px;"> </span>
Then we studied how to use functions like while, if and for:
<span style="font-size: 13.333333015441895px;">Class=6
while 1:              #also can be written as while True:
    if Class >= 10:
        break
    print(Class)
    Class = Class + 2</span>
The result is 6 , 8 .
<span style="font-size: 13.333333015441895px;"> </span>
The function while works like this :
while a condition to stop:tab process

The function if works like this:
if a condition to begin:tab processelif a condition to begin: #not necessarytab processelse: #not necessarytab process
<span style="font-size: 13.333333015441895px;"> </span>
The function for works like this:
<span style="font-size: 13.333333015441895px;"> </span>
for a variable in a range:tab process(including changing the variable)

ATTENTION:

  1. The indentations (tab, or four spaces) in these codes are very important!
  2. Don't forget the colons, or there will be syntax error!

The function import is often used at the beginning of the code, it provides packages of functions that can be used later on. For example:
<span style="font-size: 13.333333015441895px;"> </span>
<span style="font-size: 13.333333015441895px;">>>> import math
>>> math.pi
3.141592653589793
>>> math.cos(math.pi)
-1.0</span>
Here, math.pi is a setted variable carrying the value 3.141592653589793, so we can simply use it in our code.

By the way, we were introduced to a useful function: len(). It can calculate the length of a string. For example:
<span style="font-size: 13.333333015441895px;"> </span>
<span style="font-size: 13.333333015441895px;">>>> a='qwerty'
>>> len(a)
6</span>

After this, we were trying to use more operators other than +, -, *, /, and **. These are and %. a b means to count how many bs are in a. For example 11 // 3 gives 3. % means to calculate the remainder after the division, for example 11%3 gives 2. These to operators are useful for converting millions of seconds into hous, minutes and remaining seconds.


At the last of chapter 2, we learnt to let our programs receive inputs by using the function input(). We should always keep in mind that what we type in is always treated as a string. So if we want to use it as an integer, we should first change its datatype by applying the converter int(). This can be done in the same line as input. Also, we can enter some instructions in the parentheses after input to tell the typer what kind of data we want him to give. Here's an example:
<span style="font-size: 13.333333015441895px;"> </span>
<span style="font-size: 13.333333015441895px;">>>> a = int(input('Please enter an integer  '))
'Please enter an integer'  3
>>> a
3
>>> type(a)
<class 'int'></span>
Combining what we'd learnt, we can then solve the time conversion problem:
<span style="font-size: 13.333333015441895px;">t=int(input('seconds=  '))
print(t//(3600*24),' days',(t%(3600*24))//3600,' hours ',(t%3600)//60,'minutes and ',t%60,'seconds')
 
</span>
At the end of the class, we began chapter 3, turtles. The 'turtle' is a package that is needed to be imported as mentioned before. There are so many fancy features in this package and thus we can create fantastic drawings.
<span style="font-size: 13.333333015441895px;"> </span>
The basic code is:
<span style="font-size: 13.333333015441895px;">import turtle
wn = turtle.screen()
alex = turtle.Turtle()
alex.forward(100)
alex.left(90)</span>
We can explore more when we are doing our homework! Resource link
https://docs.python.org/3/library/turtle.html#compound-shapes

Have fun!